define(function (require, exports) {
	var pubjs = require('pubjs');
	var $ = require('jquery');
	var grid = require('@base/highGrid');
	var tip = require('@base/tip');
	var util = require('util');

	var Base = grid.base.extend({
		init: function (config) {
			var metrics = [];
			var map = pubjs.config('channelMap');
			util.each(pubjs.config('aptitude_group'), function (exchange_id) {
				metrics.push({
					name: exchange_id,
					render: 'renderStatus',
					align: 'center',
					width: 50,
					param: exchange_id,
					text: map[exchange_id] || LANG('未知渠道')
				});
			});

			config = pubjs.conf(config, {
					url: 'qualification/list',
					// 移除url
					urlRemove: 'qualification/remove',
					reqType: 'websocket',
					hasTab: false,
					hasAmount: false,
					hasSearch: false,
					hasSwitch: false,
					hasExport: false,
					hasMenu: true,
					hasRefresh: true,
					hasSubGrid: false,
					hasSelect: false,
					hasPager: true,
					auto_load: true,
					cols: [
						{name: '_id', text: LANG("ID"), width: 60, align: 'center', sort: true},
						{name: "UserName", text: LANG("广告主名称"), sort: false, align: 'center', type: "index", width: 250, render: 'renderName'}
					],
					metrics: [
						{name: "SiteName", text: LANG("网站名"), sort: false, align: 'center', width: 150, render: 'renderName'},
						{name: "SiteUrl", text: LANG("网址"), sort: false, align: 'left', width: 150, render: 'renderName'}
					].concat(metrics),
					param: {
						order: 'CreateTime|-1'
					},
					gridName: 'aptitudeVerify'
				}
			);

			this.Super('init', arguments);
		},
		afterBuild: function () {
			this.Super('afterBuild', arguments);
			this.uiProxy(this.getDOM(), '.rejectReason', 'mouseenter mouseleave', 'eventRejectReason');
		},
		renderName: function (i, val, row, con) {
			val = LANG(val) || '';
			return '<div class="text-truncate" title="' + val + '">' + val + '</div>';
		},
		// 审核栏渲染函数
		renderStatus: function (i, val, row, col) {
			var info = row.Info;
			var v = util.find(info, col.param, "ExchangeId");
			if (v && !v.IsDeleted) {
				switch (v.Status) {
					case 2 :
						return '<div title="' + LANG("部分通过") + '"class="icon done rejectReason" data-info="' + (v.RejectReason || '-') + '"></div>';
					case 1 :
						return '<div title="' + LANG("通过") + '" class="icon icon-checkbox-in font-success"></div>';
					case 0 :
						return '<div title="' + LANG("待审核") + '" class="icon uk-icon-clock-o"></div>';
					case -2 :
						return '<div title="' + LANG("审核中") + '" class="icon uk-icon-clock-o"></div>';
					case -1 :
						var html = '<div title="' + LANG("未通过") + '" class="icon icon-close font-danger ';
						html += (v.RejectReason && v.RejectReason.indexOf('系统判断：') === 0) ? 'sys' : 'err';
						html += '" data-info="' + util.html(v.RejectReason) + '"/>';
						return html;
				}
			} else {
				return null;
			}
		},
		/**
		 * 鼠标事件-展示素材审核失败的原因
		 */
		eventRejectReason: function (evt, elm) {
			var rejectTip = this.get('rejectReasonTip');
			switch (evt.type) {
				case 'mouseenter':
					elm = $(elm);
					var rejectReason = elm.attr("data-info");

					if (!rejectTip) {
						rejectTip = this.create('rejectReasonTip', tip.base, {
							'width': 300,
							'pos': 'ml',
							'anchor': elm,
							'autoHide': 1,
							'outerHide': 1
						});
					} else {
						rejectTip.update({anchor: elm});
					}
					rejectTip.setData(rejectReason);
					rejectTip.show();
					break;
				case 'mouseleave':
					if (rejectTip) {
						rejectTip.hide(500);
					}
					break;
			}
			return false;
		},
		// 移除一行
		removeItem: function (id, data) {
			var self = this;

			pubjs.confirm(LANG('真的要删除 (%1) 资质吗?', data.UserName), function (isOk) {
				if (isOk) {
					pubjs.mc.send(self.getConfig('urlRemove'), {_id: id}, self.afterRemove.bind(self));
				}
			});
		},
		afterRemove: function (err, data) {
			if (err) {
				pubjs.error(err.message);
				return;
			}
			// 刷新列表
			this.reload();
		},
		// 操作菜单-弹出显示菜单
		onOperateMenuShow: function (ev) {
			// var row = ev.param;
			var result = [];

			// TODO: 判断是否由上级账号授权使用的资质，是只有查看操作，否具有修改和删除操作;
			if (true) {
				result.push({id: 'edit', name: LANG("修改")});
				result.push({id: 'remove', name: LANG("删除")});
			} else {
				result.push({id: 'check', name: LANG("查看")});
			}

			ev.returnValue = result;
			return false;
		},
		// 操作菜单-响应菜单操作
		onOperateMenuSelect: function (ev) {
			var op = ev.param[0];
			var data = ev.param[1];
			var id = +data._id;

			switch (op.key) {
				case 'edit':
					window.open('#admin/aptitude/edit/' + id + '?IsAgent=' + data.IsAgent);
					break;
				case 'remove':
					this.removeItem(id, data);
					break;
				case 'check':
					// TODO:
					break;
			}
			return false;
		}
	});

	exports.base = Base;
});